package com.mobilebsc.datamanager;

import java.util.ArrayList;
import java.util.List;

import com.mobilebsc.datatype.Perspective;
import com.mobilebsc.exception.NotImplementedException;
import com.mobilebsc.httpclient.callback.IAsyncTaskCompleteListener;
import com.mobilebsc.httpclient.callback.PerspectiveRestClientUsage;

public class PerspectivesDataManager implements IDataManager<Perspective>,
		IAsyncTaskCompleteListener<List<Perspective>> {

	private PerspectiveRestClientUsage restClientUsage;
	private List<Perspective> resultList;
	private List<Perspective> singleResultList;
	private IDataReadyListener dataReadyListener;

	public PerspectivesDataManager(IDataReadyListener dataReadyListener) {
		restClientUsage = new PerspectiveRestClientUsage(this);
		resultList = null;
		singleResultList = null;
		this.dataReadyListener = dataReadyListener;
	}

	public void getAll() {
		restClientUsage.getAll();
	}

	public void getById(int id) {
		restClientUsage.getById(id);
	}

	public List<Perspective> loadAll() {
		if (resultList != null) {
			return resultList;
		} else {
			restClientUsage.getAll();
			return new ArrayList<Perspective>();
		}
	}

	public List<Perspective> loadById(int id) {
		if (resultList != null) {
			for (Perspective p : resultList) {
				if (id == p.getId()) {
					singleResultList = new ArrayList<Perspective>();
					singleResultList.add(p);
					break;
				}
			}
		} else {
			restClientUsage.getAll();
			singleResultList = new ArrayList<Perspective>();
		}

		return singleResultList;
	}

	@Override
	public void onTaskComplete(List<Perspective> result) {
		resultList = result;

		// do something with ws response
		dataReadyListener.dataReady();
	}

	@Override
	public void add(Perspective element) {
		throw new NotImplementedException();
		
	}

	@Override
	public void delete(Perspective element) {
		throw new NotImplementedException();
		
	}
}